package org.kinglone.back.comment.action;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kinglone.back.comment.model.Comment;
import org.kinglone.back.article.model.Article;
import org.kinglone.back.article.service.ArticleService;
import org.kinglone.back.comment.service.CommentService;
import org.kinglone.common.util.DateUtils;
import org.kinglone.common.util.JSONMessageView;
import org.kinglone.common.util.PageQuery;
import org.kinglone.common.util.PageResults;
import org.kinglone.common.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("comment")
public class CommentAction  {

	
   @Resource(name="commentService")
   private CommentService commentService;
   
   @Resource(name="articleService")
   private ArticleService articleService;
	
	//跳转到评论页面
	@RequestMapping("/comment.jspx")
	public ModelAndView comment(HttpServletRequest request) {
		
		ModelAndView mv = new ModelAndView("/WEB-INF/jsp/back/comment/commentList.jsp");
		return mv;
	}
	
	//查询所有的评论
		@RequestMapping("/seltCommentList.jspx")
		public JSONMessageView seltCommentList(HttpServletRequest request) {
			JSONMessageView json = new JSONMessageView(-1, "查询失败!", null);
			try {
				String pageNo = request.getParameter("pageNo"); 
				String pageSize = request.getParameter("pageSize"); 	
				PageQuery pageQuery=new PageQuery();
				pageQuery.setStart((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
				pageQuery.setLength(Integer.parseInt(pageSize));
				PageResults<Comment> pageResults = commentService.commentList(pageQuery);
				System.out.println("ppppp:"+pageResults.getTotalCount());
				JSONArray totalJsonArray = new JSONArray();
				JSONObject totalJsonObject = new JSONObject();
				JSONArray jsonArray=new JSONArray();												
				for(Comment comment : pageResults.getResults()){														
					JSONObject dataTemp = new JSONObject();		
					dataTemp.put("sysid", comment.getSysid());
					dataTemp.put("createTime", comment.getCreateTime());
					dataTemp.put("nickname", comment.getNickname());
					dataTemp.put("email", comment.getEmail());
					dataTemp.put("websiteUrl", comment.getWebsiteUrl());
					dataTemp.put("content", comment.getContent());
					dataTemp.put("isDisplay", comment.getIsDisplay());
					String articleId = comment.getArticle().getSysid();
					dataTemp.put("title",articleService.get(articleId).getTitle());
					jsonArray.add(dataTemp);
				}
				totalJsonObject.put("pagetotal", pageResults.getPageCount());//总页数
				totalJsonObject.put("info", jsonArray);
				totalJsonArray.add(totalJsonObject);
				json.setCode(0);
				json.setMessage("查询成功!");
				json.setContent(totalJsonArray);
				return json;
			} catch (Exception e) {
				e.printStackTrace();
				return new JSONMessageView(-1, "查询失败!", null);
			}	
		}
	
	//显示或者隐藏评论
		@RequestMapping("/isDisplay.jspx")
		public JSONMessageView isDisplay(HttpServletRequest request,String id) {			
			JSONMessageView json = new JSONMessageView(-1, "操作失败!", null);
			try {
				//根据id查询评论，如果isDisplay字段等于Y,改成N				
				Comment comment = commentService.get(id);
				if(comment.getIsDisplay().equals("Y")){
					comment.setIsDisplay("N");
				}else{
					comment.setIsDisplay("Y");
				}
				commentService.update(comment);
				json.setCode(0);
				return json;
				
			} catch (Exception e) {
				e.printStackTrace();
				json.setCode(-1);
				json.setMessage("操作失败!");
				return json;
			}						
		}
		//删除单个评论
		@RequestMapping("/delComment.jspx")
		public JSONMessageView delComment(HttpServletRequest request) {
			JSONMessageView json = new JSONMessageView(-1, "删除失败!", null);
			try {
				String commentId = request.getParameter("commentId");
				 commentService.deleteById(commentId);								
				json.setCode(0);
				json.setMessage("删除成功!");				
				return json;									
			} catch (Exception e) {
				e.printStackTrace();
				return new JSONMessageView(-1, "操作失败!!", null);
			}		
		}
		

		/**
		 * 提交评论信息
		 * @param request
		 * @param
		 * @return
		 */
		@RequestMapping("/subComment.jspx")
		public JSONMessageView subComment(HttpServletRequest request,HttpSession session) {
			JSONMessageView json = new JSONMessageView(-1, "评论失败!", null);
			try {
				String vcode = request.getParameter("code");
				String sessionVcode = (String) session.getAttribute("vcode");			
				if (!vcode.equals(sessionVcode)) {
					json.setCode(-1);
					json.setMessage("您输入的验证码不正确,请核实!");
					return json;
				}
				session.removeAttribute("vcode");
				String articleId = request.getParameter("articleId");
				String nickname = request.getParameter("nickname");
				String email = request.getParameter("email");
				String websiteUrl = request.getParameter("websiteUrl");
				String content = request.getParameter("content");
				
				Article article = articleService.get(articleId);
				String isComment = article.getIsComment();
				if(isComment.equals("N")) {
					json.setCode(-1);
					json.setMessage("此文章禁止评论!");
					return json;
				}
								
				if(StringUtil.isNullOrEmpty(nickname)){
					json.setCode(-1);
					json.setMessage("请输入名称!");
					return json;
				}
				if(StringUtil.isNullOrEmpty(email)){
					json.setCode(-1);
					json.setMessage("请输入邮箱!");
					return json;
				}
				if(StringUtil.isNullOrEmpty(content)){
					json.setCode(-1);
					json.setMessage("请输入内容!");
					return json;
				}
				Comment comment = new Comment();
				comment.setCreateTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
				comment.setArticle(articleService.get(articleId));
				comment.setEmail(email);
				comment.setContent(content);
				comment.setIsDisplay("Y");
				comment.setWebsiteUrl(websiteUrl);
				comment.setNickname(nickname);
				commentService.save(comment);
				json.setCode(0);
				json.setMessage("评论成功!");
				return json;				
			} catch (Exception e) {
				e.printStackTrace();
				return new JSONMessageView(-1, "系统错误!", null);
			}		
		}
	
	
}
